

import UIKit
import SnapKit



//OC里面的代理实现步骤
//1.定义一个代理
//2.设置代理属性(weak)
//让代理属性去调用方法
//3.适当的地方,实现代理方法


//注意点
//关键字 protocol
//一定继承自 NSObjectProtocol  不然代理属性不能使用 weak

 // MARK: - 代理方法

//@objc    // optional    如果用必须加!
protocol YYVisitorViewDelegate: NSObjectProtocol{


    func didRegisterMethod()

 //   optional
    func didLoginMethod()
}

class YYVisitorView: UIView {

   /*
    自定义视图三部曲
    
    第一步:重写父类构造方法
    第二步:懒加载
    第三步:添加子控件和设置约束
    */

    //anyobject SWIFT    nsobject OC   所有类的祖宗

 // MARK: - 接口文档需要注意
    //接口文档
    //先要了解接口文档
    //知道 URL
    //知道请求方式
    //查看请求参数,重点看必选
    //返回参数
    //可选实例



    //三:oauth认证
    //显示一个 web 页面
    //在 web 页面里面输入用户名和密码
    //返回一个 URL, 包含 CODE
    //通过 AFN 请求,将 CODE 发送给服务器换取 TOKEN
    //有了 token. 就可以访问有限的资源了





    weak var delegate:YYVisitorViewDelegate?


    //往回属性传值  定义一个闭包属性
    var registerClosure:(()->())?

     // MARK: - 第一步: 重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件和设置约束
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 第三步: 添加子控件和设置约束

    private func setupUI(){
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)

        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(bgImageView)


        addSubview(noticeLabel)
        addSubview(registerButton)
        addSubview(loginButton)

        bgImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

        turnImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }

        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
        }

        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(noticeLabel.snp_left)
            make.top.equalTo(noticeLabel.snp_bottom).offset(10)

            make.width.equalTo(100)
            make.height.equalTo(30)
        }

        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            make.top.equalTo(noticeLabel.snp_bottom).offset(10)

            make.width.equalTo(100)
            make.height.equalTo(30)
        }

        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }

    }

    func setupInfo(title: String, image: String?){
        if let image = image{
            noticeLabel.text = title
            bgImageView.image  = UIImage(named: image)

            turnImageView.hidden = true
        }else{
            turnImageView.hidden = false

            //开始动画
            startAnimation()
        }
    }
     // MARK: - 第二步:懒加载控件
    private lazy var maskImageView: UIImageView = {

        let img = UIImageView()

        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")

        return img
    }()

    //共有图片背景
    private lazy var bgImageView: UIImageView = {

        let img = UIImageView()

        img.image = UIImage(named: "visitordiscover_feed_image_house")

        return img
    }()


    //首页转动图片
    private lazy var turnImageView: UIImageView = {

        let img = UIImageView()

        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")

        return img
    }()


    //提示 label
    private lazy var noticeLabel: UILabel = {
        let label = UILabel()

        label.textAlignment = .Center

        label.font = UIFont.systemFontOfSize(15)

        label.textColor = UIColor.darkGrayColor()

        label.text = "关注一些人,回这里看看有什么惊喜"

        label.numberOfLines = 0

        return label
    }()


    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)

        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)

        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

        button.addTarget(self, action: "showRegister", forControlEvents: UIControlEvents.TouchUpInside)

        return button

    }()

    //登陆按钮
    private lazy var loginButton: UIButton = {

        let button = UIButton(type: UIButtonType.Custom)

        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)

        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

        button.addTarget(self, action: "showLogin", forControlEvents: UIControlEvents.TouchUpInside)

        return button
    }()

     // MARK: - 首页动画
    private func startAnimation(){


        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //转动
        animation.toValue = 2 * M_PI
        //时间
        animation.duration = 10.0
        //次数
        animation.repeatCount = MAXFLOAT

        animation.removedOnCompletion = false
        //添加到图片的 layer
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }

     // MARK: - 按钮的点击事件
    //运行是找不到私有方法的, 要在 private 之前加一个@ objc
    @objc private func showRegister(){
  //      registerClosure?()



        delegate?.didRegisterMethod()


    }


    @objc private func showLogin(){


        delegate?.didLoginMethod()

    }

    //逆传    block 闭包
    //正传
}
